/**
 * Created by chengys
 */
package fyp.spedmew;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.List;

public class RJobDetailActionBarActivity extends ActionBarActivity {

    Bundle jobData = new Bundle();
    String title, desc, shipDate, price, pic,id;
    String pickupLoc, deliveryLoc, recipientNo, customInstruction;
    Uri image;
    GoogleMap googleMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rjob_detail_action);
        createMapView();
        addMarker();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Intent in = getIntent();
        jobData = in.getExtras();

        title = jobData.getString("name");
        desc = jobData.getString("descr");
        shipDate = jobData.getString("date");
        price = jobData.getString("price");
        pic = jobData.getString("pic");
        pickupLoc = jobData.getString("pickupLoc");
        deliveryLoc = jobData.getString("dropoffLoc");
        recipientNo = jobData.getString("recipientNo");
        customInstruction = jobData.getString("customIns");
        id = jobData.getString("id");

        // image = Uri.parse( jobData.getString("pic"));
        image = Uri.parse( "pic1");
        ImageView imageView = (ImageView) findViewById(R.id.SimgView);
        imageView.setImageURI(image);

        ((TextView)findViewById(R.id.nameTxt)).setText(title);
        ((TextView)findViewById(R.id.descTxt)).setText(desc);
        ((TextView)findViewById(R.id.dateTxt)).setText(shipDate);
        ((TextView)findViewById(R.id.priceTxt)).setText(price);
        ((TextView)findViewById(R.id.pickupTxt)).setText(pickupLoc);
        ((TextView)findViewById(R.id.dropoffTxt)).setText(deliveryLoc);
        ((TextView)findViewById(R.id.recipientTxt)).setText(recipientNo);
        ((TextView)findViewById(R.id.customTxt)).setText(customInstruction);


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_create__shipment5, menu);
        return true;
    }


    private void createMapView(){
        /**
         * Catch the null pointer exception that
         * may be thrown when initialising the MapActivity
         */
        try {
            if(null == googleMap){
                googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                        R.id.mapView)).getMap();

                /**
                 * If the MapActivity is still null after attempted initialisation,
                 * show an error to the user
                 */
                if(null == googleMap) {
                    Toast.makeText(getApplicationContext(),
                            "Error creating MapActivity",Toast.LENGTH_SHORT).show();
                }
            }
        } catch (NullPointerException exception){
            Log.e("mapApp", exception.toString());
        }
    }

    /**
     * Adds a marker to the MapActivity
     */
    private void addMarker(){


        LatLngBounds.Builder builder = new LatLngBounds.Builder();


        ParseQuery<ParseObject> query = ParseQuery.getQuery("Location");
        query.whereEqualTo("JobID", "sj");
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> LocationList, ParseException e) {

                if (e == null) {
                    Number _Latitude = LocationList.get(0).getNumber("Latitude");

                    Number _Longitude = LocationList.get(0).getNumber("Longitude");

                    LatLng location = new LatLng((Double) _Latitude, (Double) _Longitude);

                    /** Make sure that the MapActivity has been initialised **/
                    if (null != googleMap) {
                        googleMap.addMarker(new MarkerOptions()
                                        .position(location)
                                        .title("Marker")
                                        .draggable(true)
                        );
                    }


                    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 15));


                    Log.d("score", "Retrieved " + LocationList.size() + " scores");
                } else {
                    Log.d("score", "Error: " + e.getMessage());
                }
            }
        });




        //   LatLng location = new LatLng(1.4431087,103.78540406);

        /** Make sure that the MapActivity has been initialised **/
        //       if(null != googleMap){
        //          googleMap.addMarker(new MarkerOptions()
        //                         .position(location)
        //                         .title("Marker")
        //                         .draggable(true)
        //       );
        //    }


        //      googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 15));


    }





    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

   }
 